package com.sihan.swvp.action.jc;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.jc.JcDsdw;
import com.sihan.swvp.service.jc.JcDsdwService;
import com.sihan.swvp.service.sys.LogService;

@Controller
@RequestMapping("/jc/JcDsdwAction")
public class JcDsdwAction extends BaseAction{

	@Autowired
	private JcDsdwService jcDsdwService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping("/getJcDsdw")
	public Object getJcDsdw(@Param("code")String code,@Param("dsdwName")String dsdwName,@Param("page")Integer page,@Param("limit")Integer limit){
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("code", code);
		params.put("dsdwName", dsdwName);
		return pageData(jcDsdwService.getJcDsdw(params,page,limit));
	}
	
	@ResponseBody
	@RequestMapping("/addJcDsdw")
	public Object addJcDsdw(JcDsdw bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcDsdwService.getJcDsdwValidate(null, bean.getDsdwName(),null);
			boolean isCodeOk=jcDsdwService.getJcDsdwValidate(bean.getCode(), null,null);
			if(isDsdwNameOk){
				return failMsg("该地市单位名称已存在");
			}else if(isCodeOk){
				return failMsg("该地市单位编码已存在");
			}
				jcDsdwService.addJcDsdw(bean);
				logService.addUserLog("添加地市单位", "地市单位", getLoginUser().getId(), "添加地市单位："+bean.getDsdwName(),getRemoteAddr(),1,0);
				return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加地市单位", "地市单位", getLoginUser().getId(), "添加地市单位："+bean.getDsdwName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/updateJcDsdw")
	public Object updateJcDsdw(JcDsdw bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcDsdwService.getJcDsdwValidate(null, bean.getDsdwName(),bean.getId());
			boolean isCodeOk=jcDsdwService.getJcDsdwValidate(bean.getCode(), null,bean.getId());
			if(isDsdwNameOk){
				return failMsg("该地市单位名称已存在");
			}else if(isCodeOk){
				return failMsg("该地市单位编码已存在");
			}
			jcDsdwService.updateJcDsdw(bean);
			logService.addUserLog("修改地市单位", "地市单位", getLoginUser().getId(), "修改地市单位："+bean.getDsdwName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改地市单位", "地市单位", getLoginUser().getId(), "修改地市单位："+bean.getDsdwName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteJcDsdw")
	public Object deleteJcDsdw(@Param("id")Integer id,String dsdwName){
		try {
			jcDsdwService.deleteJcDsdw(id);
			logService.addUserLog("删地市单位", "地市单位", getLoginUser().getId(), "删除地市单位："+dsdwName,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删地市单位", "地市单位", getLoginUser().getId(), "删除地市单位："+dsdwName,getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
}
